package com.sunmi.sbs.commons.utils;

import org.dom4j.DocumentException;
import org.dom4j.Element;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * author: fanjia
 * date: 2020/10/16 18:18
 * file: TestPlanUtils.java
 */


public class TestPlanUtils extends XMLUtils {
    public static String test() {
        return ("this is a test func");
    }

    public static String apiNamePattern(String apiName){
        String name =apiName;
        System.out.println(apiName);;

        String pattern = ".*(/.*/.*)$";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(name);
        if (m.find()){
            System.out.println(m.group(0));
            System.out.println(m.group(1));

        }
        return m.group(1);
    }

    public static List<LinkedHashMap> getTestPlan(String filePath) throws DocumentException {
        Element root = getRoot(filePath);
        Element rootHashTree = getRootHashTree(root);
        List<String> threadList = getElementsName(getKidNodesByType(rootHashTree, "ThreadGroup"));
//        System.out.println(threadList);

        List<LinkedHashMap> threadHashMaps = new ArrayList<>();
        if (threadList.size() != 0) {
            for (String threadName : threadList) {
//                System.out.println("threadName " + threadName);
                LinkedHashMap<String, List> threadHashMap = new LinkedHashMap<>();

                Element threadHashTree = getHashTreeNodeAfterElement(rootHashTree, "ThreadGroup", threadName);
                List<String> apiList = getElementsName(getKidNodesByType(threadHashTree, "GenericController"));
//                System.out.println(apiList);

                List<LinkedHashMap> apiHashMaps = new ArrayList<>();
                if (apiList.size() != 0) {
                    for (String apiName : apiList) {
                        LinkedHashMap<String, List> apiHashMap = new LinkedHashMap<>();
                        Element caseHashTree = getHashTreeNodeAfterElement(threadHashTree, "GenericController", apiName);
                        List<String> caseList = getElementsName(getKidNodesByType(caseHashTree, "GenericController"));
//                        System.out.println(caseList);

                        List<LinkedHashMap> caseHashMaps = new ArrayList<>();
                        if (caseList.size() != 0) {
                            for (String testcase : caseList) {
//                                System.out.println("case " +  testcase);
                                LinkedHashMap<String, List> caseHashMap = new LinkedHashMap<>();
                                Element stepHashTree = getHashTreeNodeAfterElement(caseHashTree, "GenericController", testcase);
                                List<String> stepList = getElementsName(getKidNodesByType(stepHashTree, "GenericController"));
//                                System.out.println(stepList);
                                caseHashMap.put(testcase, stepList);
                                if (stepList.size() != 0) {
                                    for (String step : stepList) {
//                                        System.out.println("step " + step);
//                                        Element beanShellHashTree = getHashTreeNodeAfterElement(stepHashTree, "GenericController", step);
//                                        String beanShell = getElementName(getKidNodeByType(beanShellHashTree, "BeanShellSampler"));
//                                        System.out.println("beanShell " + beanShell);
                                    }
                                }
//                                System.out.println(caseHashMap);
                                caseHashMaps.add(caseHashMap);

                                apiHashMap.put(apiName, caseHashMaps);
                            }
                        }
//                        System.out.println(apiHashMap);
                        apiHashMaps.add(apiHashMap);
                        threadHashMap.put(threadName, apiHashMaps);

                    }
                }
//                System.out.println(threadHashMap);
                threadHashMaps.add(threadHashMap);
            }
//            System.out.println(threadHashMaps);

        }
        return threadHashMaps;
    }

    public static String getApi(List<LinkedHashMap> testPlan, String groupName) {
        for (LinkedHashMap thread : testPlan) {
            Iterator threadIter = thread.entrySet().iterator();
            while (threadIter.hasNext()) {
                Map.Entry threadEntry = (Map.Entry) threadIter.next();
                Object threadK = threadEntry.getKey();
                Object threadV = threadEntry.getValue();
                if (groupName.equals(threadK)) {
//                    System.out.println("当前线程组 " + threadK);
                    List<LinkedHashMap> apiList = (List<LinkedHashMap>) threadV;
//			System.out.println(apiList.toString());
                    int apiCount = apiList.size();
//                    System.out.println("共" + String.valueOf(apiCount) + "个接口");
                    for (LinkedHashMap api : apiList) {
                        Iterator apiIter = api.entrySet().iterator();
                        while (apiIter.hasNext()) {
                            Map.Entry apiEntry = (Map.Entry) apiIter.next();
                            Object apiK = apiEntry.getKey();
                            Object apiV = apiEntry.getValue();
//                            System.out.println("当前接口 "+ apiK);
//					System.out.println(apiK + " = " + apiV);
                            List<LinkedHashMap> caseList = (List<LinkedHashMap>) apiV;
                            System.out.println(caseList.toString());
                            int caseSize = caseList.size();
//                            System.out.println("共" + String.valueOf(caseSize) + "个case");
                            for (LinkedHashMap testcase : caseList) {
                                Iterator caseIter = testcase.entrySet().iterator();
                                while (caseIter.hasNext()) {
                                    Map.Entry caseEntry = (Map.Entry) caseIter.next();
                                    Object caseK = caseEntry.getKey();
                                    Object caseV = caseEntry.getValue();
//							System.out.println("当前case " + caseK);
//                                    System.out.println(caseK + " = " + caseV);
                                    List<String> stepList = (List<String>) caseV;
//                                    System.out.println(stepList.toString());
                                    int stepSize = stepList.size();
//                                    System.out.println("共" + String.valueOf(stepSize) + "个step");

                                    for (String step : stepList) {
//                                        System.out.println(step);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return null;

    }


    public static String getVoid(List<LinkedHashMap> testPlan, String groupName) {
        ArrayList<LinkedHashMap> apiList = gethashVByKey(testPlan, groupName);
//        System.out.println(apiList.toString());
        int apiCount = apiList.size();
        System.out.println("共" + String.valueOf(apiCount) + "个接口");
        int i;
        for (i = 0; i < apiCount; i++) {
            LinkedHashMap api = getHashMapFromHashMapList(apiList, i);
            System.out.println(api);
            ArrayList<LinkedHashMap> caseList = getHashList(api);
//            System.out.println(caseList.toString());
            int caseCount = caseList.size();
//            System.out.println("共" + String.valueOf(caseCount) + "个case");
            int k;
            for (k = 0; k < caseCount; k++) {
                LinkedHashMap testcase = getHashMapFromHashMapList(caseList, k);
                System.out.println(testcase);
                List<String> stepList = getList(testcase);
//                System.out.println(stepList.toString());
                int stepCount = stepList.size();
                System.out.println("共" + String.valueOf(stepCount) + "个step");
                int j;
                for (j = 0; j < stepCount; j++) {
                    String step = getStringFromStringList(stepList, j);
                    System.out.println(step);
                }
            }
        }
        return null;
    }

    public static List<String> getList(LinkedHashMap parentList) {
        Iterator iter = parentList.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            Object K = entry.getKey();
            Object V = entry.getValue();
            return (List<String>) V;
        }

        return null;
    }

    public static ArrayList<LinkedHashMap> getHashList(LinkedHashMap parentMap) {
        Iterator iter = parentMap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            Object K = entry.getKey();
            Object V = entry.getValue();
            return (ArrayList<LinkedHashMap>) V;
        }

        return null;
    }


    public static ArrayList<LinkedHashMap> gethashVByKey(List<LinkedHashMap> parentList, String Key) {
//        System.out.println(parentList.toString());
        for (LinkedHashMap hashMap : parentList) {
            Iterator iter = hashMap.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                Object K = entry.getKey();
                Object V = entry.getValue();
//                System.out.println(K + " = " + V);
                if (Key.equals(K)) {
                    return (ArrayList<LinkedHashMap>) V;
                }
            }
        }
        return null;
    }


    public static String getStringFromStringList(List<String> List, int count) {
        return List.get(count);
    }


    public static LinkedHashMap getHashMapFromHashMapList(ArrayList<LinkedHashMap> list, int count) {
        LinkedHashMap lhm = list.get(count);
        return lhm;
    }

    public static String getStepName(ArrayList apiList, int apiNum, int caseNum, int stepNum) {
        int apiCount = apiList.size();
        if (apiNum < apiCount) {
            LinkedHashMap api = getHashMapFromHashMapList(apiList, apiNum);
            ArrayList caseList = getHashList(api);
            int caseCount = caseList.size();
            if (caseNum < caseCount) {
                LinkedHashMap testcase = getHashMapFromHashMapList(caseList, caseNum);
                List stepList = getList(testcase);
                int stepCount = stepList.size();
                if (stepNum < stepCount) {
                    String step = getStringFromStringList(stepList, stepNum);
                    String apiName =api.keySet().toString();
                    apiName = "[" + apiNamePattern(apiName);
                    String name = apiName +" [用例"+ (caseNum+1) + "] "+ testcase.keySet().toString();

                    return name + " " + "[步骤"+ (stepNum+1) + "] " +step;
                } else {
                    return "stepErr";
                }
            } else {
                return "caseErr";
            }
        } else {
            return "apiErr";
        }
    }

}
